import ToastComponent from './vue-toast.vue'

let Toast = {}
Toast.install = function (Vue, options) {

	var opt = {
    defaultType:'center',
    duration:'3000'
  }

	for (var key in options) {
		opt[key] = options[key]
	}

	Vue.prototype.$toast = function (message, option) {

		let callback = '';

		if (typeof option == 'object') {
			for (var key in option) {
				opt[key] = option[key]
			}
		} else if (typeof option == 'function') {
			callback = option
		}

		const ToastController = Vue.extend(ToastComponent)  // 返回一个Vue实例
		var instance = new ToastController().$mount(document.createElement('div'))
		instance.message = message;
		instance.visible = true;
		document.body.appendChild(instance.$el);

	
		setTimeout(() => {
			instance.visible = false;
			setTimeout(() => {
				document.body.removeChild(instance.$el);
				callback && callback();
			}, 500)
			
		}, opt.duration);
	};
	['show', 'success', 'info', 'error'].forEach((type) => {
		Vue.prototype.$toast[type] = function (tips, option) {
			return Vue.prototype.$toast(tips, option)
		}
	})
};
if (typeof window !== 'undefined' && window.Vue) {
	window.Vue.use(Toast)
}

export default Toast;